<?php
namespace app\admin\controller;
/**
 * 需要登录的抽象基类
 * 不需要选择操作的站点
 */
abstract class Auth extends Base{
    
    
    private $userObject; // 当前用户登录对象    
    protected $userId;   // 当前登录用户ID [ $adminid ]
    
    public function __construct(){
        parent::__construct();
        $this->userObject = $this->passportObject->checkLogin($this->role);        
        if(!$this->userObject) return $this->redirect('/passport/login');   
        $this->userId = $this->userObject->userId;        
        $this->checkUserPower();        
        $this->formHash();
    }
    
    /**
     * 表单提交校验
     */
    protected function formHash(){
        if( request()->isPost() ){
            $getFormHash = cookie('c_hash');
            $formHash = session('c_sys_fromhash');
            if( !$formHash || $formHash != trim($getFormHash) ) $this->result('',2,'正在处理中,请别着急...');
        }
        $newHash = microtime(true).mt_rand(1000,9999);
        cookie('c_hash',$newHash);
        session('c_sys_fromhash',$newHash);
    }
    
    /**
    * 获取用户对象
    */
    protected function getUser($isSystem = false ){
        if( $isSystem ){
            if( !$this->userObject ){
                return \logic\user\User::getInstance('','system');
            }else{
                return $this->userObject->getSystemUser();
            }            
        } 
        if( !$this->userObject ){
            $this->userObject = \logic\user\User::getInstance($this->adminid,$this->role);
        }        
        return $this->userObject;
    }
    
    /**
     * 判断用户的访问权限
     */
    private function checkUserPower(){
        return true;
        $powerList = array( '/index/index','/sysmenu/ajaxmenulist' , '/sysmenu/ajaxmenulist' );
        $node = $this->getAccessNode();
        if( in_array( $node , $powerList ) ) return true;
        $result = \logic\user\UserPower::checkPower($this->getUser(),$node);
        if( !$result ) $this->error('没有权限');
    }

    
    
   /**
    * 收集新增节点
    */
    public function __destruct(){
        return true;
        $debug = config('app.app_debug');
        if( !$debug )  return true;
        $request = request();
		if(method_exists($this,$request->action())){
            $baseUrl = $this->getAccessNode();
            $parmas = array('plat'=>$this->plat, 'menuurl'=>$baseUrl);
            $Menu = new \logic\sys\power\Menu($this->getUser(true));
            $info = $Menu->autoAddMenu($parmas);
		}
   }
}
